package com.bc004346.travelus.dataAccess;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

/**
 * Created by Renats on 6/29/2014.
 * data provider for airports
 */
public class AirportDataProvider extends ContentProvider {


    public static final String AUTHORITY = "com.bc004346.travelus.AirportDataProvider";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/airports");
    private static final int SUGGESTIONS_AIRPORTS = 1;
    private static final int SEARCH_AIRPORT = 2;
    private static final int GET_AIRPORTS = 3;
    UriMatcher mUriMatcher = buildUriMatcher();
    private SQLHelper mSQLHelper = null;

    private UriMatcher buildUriMatcher() {

        UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SUGGESTIONS_AIRPORTS);
        uriMatcher.addURI(AUTHORITY, "airports", SEARCH_AIRPORT);
        uriMatcher.addURI(AUTHORITY, "airports/#", GET_AIRPORTS);

        return uriMatcher;
    }

    @Override
    public boolean onCreate() {
        mSQLHelper = new SQLHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

        Cursor c = null;
        switch (mUriMatcher.match(uri)) {
            case SUGGESTIONS_AIRPORTS:
                c = mSQLHelper.getAirports(selectionArgs);
                break;
            case SEARCH_AIRPORT:
                c = mSQLHelper.getAirports(selectionArgs);
                break;
            case GET_AIRPORTS:
                String id = uri.getLastPathSegment();
                c = mSQLHelper.getAirport(id);
        }

        return c;
    }

    //<editor-fold desc="Unsupported">
    @Override
    public String getType(Uri uri) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        throw new UnsupportedOperationException();
    }
    //</editor-fold>
}
